/**
 * 
 */
package com.vector.ding.web.controller.baseinfo;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vector.ding.commons.ActionResult;
import com.vector.ding.commons.Constant;
import com.vector.ding.commons.util.BeanUtil;
import com.vector.ding.commons.util.file.UploadUtil;
import com.vector.ding.commons.util.front.BuiUtil;
import com.vector.ding.dao.mybatis.model.RzzlgsBaseInfo;
import com.vector.ding.web.service.baseinfo.RzzlgsBaseInfoMgrService;

/**
 * @author vector
 *
 */
@Controller
@RequestMapping("/baseinfo/rzzlgs")
public class RzzlgsBaseInfoMgrController {
	
	@Autowired
	private RzzlgsBaseInfoMgrService service;

	@RequestMapping(value = "/mgrpage.do", method = RequestMethod.GET)
	public ModelAndView getMgrPage() {
		return new ModelAndView("/baseinfo/rzzlgs_base_info_mgr");
	}
	
	@RequestMapping(value = "/detailPage.do", method = RequestMethod.GET)
	public ModelAndView getDetailPage(String number) {
		RzzlgsBaseInfo info = service.getBaseInfo(number);
		ModelAndView mv = new ModelAndView("/baseinfo/rzzlgs_base_info_detail");
		if (info != null) {
			mv.addAllObjects(BeanUtil.getPropertyMap(info));
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getdata.do", method = RequestMethod.POST)
	public JSONObject getData(HttpServletRequest request, 
			int pageIndex, int limit) {
		List<RzzlgsBaseInfo> data = service.getBaseInfo(pageIndex + 1, limit, request);
		
		return BuiUtil.gridAdapter(data);
	}
	
	@ResponseBody
	@RequestMapping(value = "/importData.do", method = RequestMethod.POST)
	public JSONObject importData(HttpServletRequest request,
			HttpServletResponse response) {
		String uploadPath = Constant.Upload.BASE_INF_PATH;
		
		try {
			File file = UploadUtil.getUploadFile(request, response, uploadPath);
			service.importFormFile(file);
			return ActionResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.fail("导入数据失败：" + e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/editBaseInfo.do", method = RequestMethod.POST)
	public JSONObject editBaseInfo(String action, HttpServletRequest request) {
		if (Constant.RECORD_ADD.equals(action)) {
			String comName = request.getParameter("companyName");
			RzzlgsBaseInfo checkInfo = service.getBaseInfoByName(comName);
			if(checkInfo != null){
				return ActionResult.fail("所添加公司已经存在！");
			}
			RzzlgsBaseInfo baseInfo = service.getBaseInfoBean(request);
			return service.insertBaseInfo(baseInfo);
		} else if (Constant.RECORD_UPDATE.equals(action)) {
			RzzlgsBaseInfo baseInfo = service.getBaseInfoBean(request);
			return service.updateBaseInfo(baseInfo);
		} else if (Constant.RECORD_DELETE.equals(action)) {
			String[] ids = request.getParameterValues("ids[]");
			if (null == ids)
				return ActionResult.success();

			return service.delete(ids);
		}

		return ActionResult.fail("不识别的操作！");
	}
	
}
